package cn.wolfcode.crm.web.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.wolfcode.crm.util.JSONResult;

@Controller
@RequestMapping("/login")
public class LoginController {
	
	@RequestMapping("/login")
	@ResponseBody
	public Object login(String email, String password, Model model) {
		System.out.println("email="+email);
		/**
		 * 使用Shiro编写认证操作
		 */
		//1.获取Subject
		Subject subject = SecurityUtils.getSubject();
		
		//2.封装用户数据
		UsernamePasswordToken token = new UsernamePasswordToken(email,password);
		
		JSONResult jsonResult = new JSONResult();
		//3.执行登录方法
		try {
			subject.login(token);
			//登录成功
//			return "redirect:/login/toIndex";
		} catch (Exception e) {
//			e.printStackTrace();
			//登录失败:用户名不存在
			jsonResult.mark("用户名或密码有误");
//			return "login/toLogin";
		}
		return jsonResult;
	}
	
	@RequestMapping("/toLogin")
	public String toLogin() {
		return "login";
	}
	
	@RequestMapping("/toIndex")
	public String toIndex() {
		return "index";
	}
	
	@RequestMapping("/toNopermission")
	public String toNopermission() {
		return "common/nopermission";
	}
	
}
